//
//  SCAWishSaveController.m
//  Mutal
//
//  Created by TonyStart on 16/10/22.
//  Copyright © 2016年 com.wx. All rights reserved.
//

#import "SCAWishSaveController.h"
#import "SCAlertView.h"
#import "SCHeaderView.h"
#import "SCUserInfo.h"
#import "SCAWishViewModel.h"
@interface SCAWishSaveController ()
@property(nonatomic,strong)UITextField *wishPriceField;
@property(nonatomic,strong)SCAWishViewModel *aWishViewModel;
@end

@implementation SCAWishSaveController
-(SCAWishViewModel *)aWishViewModel{
    if (!_aWishViewModel) {
        _aWishViewModel = [[SCAWishViewModel alloc] init];
    }
    return _aWishViewModel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}
-(void)setupNavgationItem{
    SCHeaderView *headerView = [[SCHeaderView alloc] init];
    [headerView.leftBtn setTitle:@"上一步" forState:UIControlStateNormal];

    [headerView.leftBtn addTarget:self action:@selector(backupPreVc) forControlEvents:UIControlEventTouchUpInside];
    headerView.centerLabel.text = @"愿望设置";
    
    [self.view addSubview:headerView];
}
-(void)setupUI{
    self.view.backgroundColor = [UIColor grayColor];
    [self setupNavgationItem];
}
-(void)viewDidAppear:(BOOL)animated{
    [self setupContent];
}
-(void)setupContent{
 
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 74, [UIScreen mainScreen].bounds.size.width, 40)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *wishLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 80, 20)];
    wishLabel.text = @"愿望金额";
    
    
    _wishPriceField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(wishLabel.frame)+2, 5, [UIScreen mainScreen].bounds.size.width - 80 - 35, 30)];
    _wishPriceField.keyboardType = UIKeyboardTypeNumberPad;
    _wishPriceField.textAlignment = NSTextAlignmentRight;
    _wishPriceField.placeholder = @"输入金额";
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_wishPriceField.frame)+2, 10, 20, 20)];
    label.text = @"元";
    
    [view addSubview:wishLabel];
    [view addSubview:_wishPriceField];
    [view addSubview:label];
    
    [self.view addSubview:view];
    
    
    UIButton *saveWishBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 40, 30)];
    
    saveWishBtn.backgroundColor = [UIColor colorWithRed:81/255.0 green:200/255.0 blue:195/255.0 alpha:1];
    [saveWishBtn setTitle:@"发布愿望" forState:UIControlStateNormal];
    [saveWishBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [saveWishBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [saveWishBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    saveWishBtn.center = self.view.center;
    saveWishBtn.layer.cornerRadius = 5;
    saveWishBtn.layer.masksToBounds = YES;
    saveWishBtn.tag = 200;
    [saveWishBtn addTarget:self action:@selector(saveWish:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *saveWarmBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 40, 30)];
    
    saveWarmBtn.backgroundColor = [UIColor colorWithRed:81/255.0 green:200/255.0 blue:195/255.0 alpha:1];
    [saveWarmBtn setTitle:@"发布到预热" forState:UIControlStateNormal];
    [saveWarmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [saveWarmBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [saveWarmBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    
    saveWarmBtn.center = CGPointMake(self.view.center.x, self.view.center.y+40);
    saveWarmBtn.layer.cornerRadius = 5;
    saveWarmBtn.layer.masksToBounds = YES;
    saveWishBtn.tag = 201;
    [saveWarmBtn addTarget:self action:@selector(saveWish:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:saveWishBtn];
    [self.view addSubview:saveWarmBtn];
    
}
-(void)saveWish:(UIButton*)sender{
    if ([_wishPriceField.text isEqualToString:@""] || [_wishPriceField.text doubleValue]<=0) {
        [SCAlertView alertViewWithTitle:@"金额不能为空且金额必须大于0"];
        return;
    }
    _wish.wish_price = _wishPriceField.text;
    //初始化其他数据
    _wish.city = @"";
    _wish.province = @"";
    //判断用户信息单例内是否有保存数据
    SCUserInfo *userInfo = [SCUserInfo defaultUser];
    
    if (!userInfo.userInfo) {
        //如果单例内种没有数据 就从Plist中取出来 然后给单例赋值
        NSDictionary *userInfo = [NSDictionary dictionaryWithContentsOfFile:[NSHomeDirectory() stringByAppendingString:@"/Documents/userInfo.plist"]];
        [SCUserInfo defaultUser].userInfo = [SCUserInfoModel userInfoWithDict:userInfo];
        
    }
    _wish.user_id = userInfo.userInfo.id;
    if (sender.tag == 200) {
        _wish.wish_type = @"1";
    }else if (sender.tag == 201){
        _wish.wish_type = @"2";
    }
    
    _wish.wish_end_time = @"2016-11-30";
    _wish.mobile = userInfo.userInfo.mobile;
    
    if (_wish) {
        [self.aWishViewModel saveAWish:_wish type:1 finish:^(BOOL isSucess) {
            if (isSucess) {
                [SCAlertView alertViewWithTitle:@"保存愿望成功!"];
            }
            
        }];
    }else{
        [SCAlertView alertViewWithTitle:@"愿望内容出错!"];
    }
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
-(void)backupPreVc{
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
}
@end
